/*
 * @Author: 李一番 
 * @Date: 2021-12-24 14:38:18 
 * @Last Modified by: 李一番
 * @Last Modified time: 2021-12-24 17:59:45
 */

import { createI18n } from 'vue-i18n' 

import elementEnLocale from 'element-plus/lib/locale/lang/en'
import elementZhLocale from 'element-plus/lib/locale/lang/zh-cn'

import enLocale from './en'
import zhLocale from './zh-cn'
import { getCookie } from '@/utils/auth'
import { App } from 'vue'

const messages = {
  en: {
    ...enLocale,
    ...elementEnLocale
  },
  'zh-cn': {
    ...zhLocale,
    ...elementZhLocale
  }
}

export const getLocale = () => {
  const cookieLanage = getCookie('language')
  if (cookieLanage) return cookieLanage
  const language = navigator.language.toLowerCase()
  const locales = Object.keys(messages)
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      return locale
    }
  }
  return 'zh-cn'
}

export const i18n = createI18n({
  locale: getLocale(),
  messages: messages
})

const setupVueI18n = (app: App<Element>) => {
  app.use(i18n)
}

export default setupVueI18n